package com.wtc.homework;

/**
 * @author 马可
 * @version 1.0
 * 编程题：交通工具
 * 2023/5/10
 * IntelliJ IDEA
 */
public class Homework05 {
    public static void main(String[] args) {
        Person person = new Person("唐僧", new Horse());
        person.common();
        person.common();
        person.common();
        person.passRiver();
        person.passRiver();
        person.common();
        person.fly();
        person.common();
        person.passRiver();
    }
}
interface Vehicles {
    void work();
}
class Horse implements Vehicles {
    @Override
    public void work() {
        System.out.println("当前交通工具：马");
    }
}
class Boat implements Vehicles {
    @Override
    public void work() {
        System.out.println("当前交通工具：船");
    }
}
class Air implements Vehicles {
    @Override
    public void work() {
        System.out.println("当前交通工具：飞机");
    }
}
class Factories {
    private static Horse horse = new Horse();

    private Factories() {
    }

    public static Horse getHorse() {
        return horse;
    }
    public static Boat getBoat() {
        return new Boat();
    }
    public static Air getAir() {
        return new Air();
    }
}
class Person {
    private String name;
    private Vehicles vehicles;

    public Person(String name, Vehicles vehicles) {
        this.name = name;
        this.vehicles = vehicles;
    }
    public void passRiver() {
        if (!(vehicles instanceof Boat)) {
            vehicles = Factories.getBoat();
        }
        vehicles.work();
    }
    public void common() {
        if (!(vehicles instanceof Horse)) {
            vehicles = Factories.getHorse();
        }
        vehicles.work();
    }
    public void fly() {
        if (!(vehicles instanceof Air)) {
            vehicles = Factories.getAir();
        }
        vehicles.work();
    }
}